package cc.autoapi.pucong.liteflow.design.controller;

import cc.autoapi.pucong.liteflow.design.core.configuration.DesignExceptionHandler;
import cc.autoapi.pucong.liteflow.design.core.configuration.LiteFlowDesignConfiguration;
import cc.autoapi.pucong.liteflow.design.mobal.JsonBean;
import cc.autoapi.pucong.liteflow.design.mobal.Valid;
import cc.autoapi.pucong.liteflow.design.mobal.dto.*;
import cc.autoapi.pucong.liteflow.design.mobal.vo.*;
import cc.autoapi.pucong.liteflow.design.service.DesignService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/flowDesign/designApi")
public class DesignApiController extends DesignController implements DesignExceptionHandler {

    public DesignApiController(LiteFlowDesignConfiguration configuration) {
        super(configuration);
    }

    @GetMapping("/getChainListByPage/{pageNum}/{pageSize}")
    public JsonBean<BasePageVo<ChainPageVo>> getChainListByPage(@PathVariable("pageNum") int pageNum,
                                                                @PathVariable("pageSize") int pageSize,
                                                                String searchKey) {
        DesignService designService = configuration.getDesignService();
        BasePageVo<ChainPageVo> chainPageVoBasePageVo = designService.getChainListByPage(pageNum, pageSize, searchKey);
        return new JsonBean<>(chainPageVoBasePageVo);
    }

    @GetMapping("/getChainList")
    public JsonBean<List<ChainPageVo>> getChainList(String searchKey) {
        DesignService designService = configuration.getDesignService();
        List<ChainPageVo> chainPageVoBasePageVo = designService.getChainList(searchKey);
        return new JsonBean<>(chainPageVoBasePageVo);
    }

    @GetMapping("/getChainInfoById/{chainId}")
    public JsonBean<ChainPageVo> getChainInfoById(@PathVariable("chainId") String chainId) {
        DesignService designService = configuration.getDesignService();
        ChainPageVo vo = designService.getChainInfoById(chainId);
        return new JsonBean<>(vo);
    }

    @GetMapping("/getChainElById/{chainId}")
    public JsonBean<String> getChainElById(@PathVariable("chainId") String chainId) {
        DesignService designService = configuration.getDesignService();
        String el = designService.getChainElById(chainId);
        return new JsonBean<>(el);
    }

    @GetMapping("/getChainDesignById/{chainId}")
    public JsonBean<ChainDesignVo> getChainDesignJsonById(@PathVariable("chainId") String chainId) {
        DesignService designService = configuration.getDesignService();
        ChainDesignVo vo = designService.getChainDesignJsonById(chainId);
        return new JsonBean<>(vo);
    }

    @PostMapping("/addChain")
    @Valid
    public JsonBean<Boolean> addChain(@RequestBody ChainInsertDto dto) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.addChain(dto);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateChainById/{chainId}")
    @Valid
    public JsonBean<Boolean> updateChainById(@PathVariable("chainId") String chainId,
                                             @RequestBody ChainUpdateDto dto) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateChainById(chainId, dto);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateChainElById/{chainId}")
    @Valid
    public JsonBean<Boolean> updateChainElById(@PathVariable("chainId") String chainId,
                                               @RequestBody ChainElUpdateDto dto) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateChainElById(chainId, dto);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateChainEnableById/{chainId}")
    @Valid
    public JsonBean<Boolean> updateChainEnableById(@PathVariable("chainId") String chainId) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateChainEnableById(chainId);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateChainDisableById/{chainId}")
    @Valid
    public JsonBean<Boolean> updateChainDisableById(@PathVariable("chainId") String chainId) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateChainDisableById(chainId);
        return new JsonBean<>(b);
    }

    @PostMapping("/deleteChainById/{chainId}")
    @Valid
    public JsonBean<Boolean> deleteChainById(@PathVariable("chainId") String chainId) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.deleteChainById(chainId);
        return new JsonBean<>(b);
    }

    @GetMapping("/getAllNodeList")
    public JsonBean<List<NodeTreeVo>> getAllNodeList(String searchKey) {
        DesignService designService = configuration.getDesignService();
        List<NodeTreeVo> chainPageVoBasePageVo = designService.getAllNodeList(searchKey);
        return new JsonBean<>(chainPageVoBasePageVo);
    }

    @GetMapping("/getScriptNodeList")
    public JsonBean<List<NodeScriptVo>> getScriptNodeList(String searchKey) {
        DesignService designService = configuration.getDesignService();
        List<NodeScriptVo> vos = designService.getScriptNodeList(searchKey);
        return new JsonBean<>(vos);
    }

    @GetMapping("/getCommonNodeList")
    public JsonBean<List<NodeVo>> getCommonNodeList() {
        DesignService designService = configuration.getDesignService();
        List<NodeVo> vos = designService.getCommonNodeList();
        return new JsonBean<>(vos);
    }

    @GetMapping("/getBooleNodeList")
    public JsonBean<List<NodeVo>> getBooleNodeList() {
        DesignService designService = configuration.getDesignService();
        List<NodeVo> vos = designService.getBooleNodeList();
        return new JsonBean<>(vos);
    }

    @GetMapping("/getForNodeList")
    public JsonBean<List<NodeVo>> getForNodeList() {
        DesignService designService = configuration.getDesignService();
        List<NodeVo> vos = designService.getForNodeList();
        return new JsonBean<>(vos);
    }

    @GetMapping("/getIteratorNodeList")
    public JsonBean<List<NodeVo>> getIteratorNodeList() {
        DesignService designService = configuration.getDesignService();
        List<NodeVo> vos = designService.getIteratorNodeList();
        return new JsonBean<>(vos);
    }

    @GetMapping("/getSwitchNodeList")
    public JsonBean<List<NodeVo>> getNodeListByType() {
        DesignService designService = configuration.getDesignService();
        List<NodeVo> vos = designService.getSwitchNodeList();
        return new JsonBean<>(vos);
    }

    @GetMapping("/getProcessNodeList")
    public JsonBean<List<NodeVo>> getProcessNodeList() {
        DesignService designService = configuration.getDesignService();
        List<NodeVo> vos = designService.getProcessNodeList();
        return new JsonBean<>(vos);
    }

    @GetMapping("/getNodeInfoById/{nodeId}")
    public JsonBean<NodeScriptVo> getNodeInfoById(@PathVariable("nodeId") String nodeId) {
        DesignService designService = configuration.getDesignService();
        NodeScriptVo vo = designService.getNodeInfoById(nodeId);
        return new JsonBean<>(vo);
    }

    @PostMapping("/addNode")
    @Valid
    public JsonBean<String> addNode(@RequestBody NodeInsertDto dto) {
        DesignService designService = configuration.getDesignService();
        String id = designService.addNode(dto);
        return new JsonBean<>(id);
    }

    @PostMapping("/updateNodeById/{nodeId}")
    @Valid
    public JsonBean<Boolean> updateNodeById(@PathVariable("nodeId") String nodeId,
                                            @RequestBody NodeUpdateDto dto) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateNodeById(nodeId, dto);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateNodeScriptById/{nodeId}")
    @Valid
    public JsonBean<Boolean> updateNodeScriptById(@PathVariable("nodeId") String nodeId,
                                                  @RequestBody NodeScriptUpdateDto dto) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateNodeScriptById(nodeId, dto);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateNodeEnableById/{nodeId}")
    @Valid
    public JsonBean<Boolean> updateNodeEnableById(@PathVariable("nodeId") String nodeId) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateNodeEnableById(nodeId);
        return new JsonBean<>(b);
    }

    @PostMapping("/updateNodeDisableById/{nodeId}")
    @Valid
    public JsonBean<Boolean> updateNodeDisableById(@PathVariable("nodeId") String nodeId) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.updateNodeDisableById(nodeId);
        return new JsonBean<>(b);
    }

    @PostMapping("/deleteNodeById/{nodeId}")
    @Valid
    public JsonBean<Boolean> deleteNodeById(@PathVariable("nodeId") String nodeId) {
        DesignService designService = configuration.getDesignService();
        Boolean b = designService.deleteNodeById(nodeId);
        return new JsonBean<>(b);
    }

    @PostMapping("/exec/{chainId}")
    public JsonBean<Object> exec(@PathVariable("chainId") String chainId,
                                 @RequestHeader HashMap<String, Object> headerMap,
                                 @RequestBody HashMap<String, Object> bodyMap,
                                 HashMap<String, Object> queryMap) {
        DesignService designService = configuration.getDesignService();
        Object vo = designService.exec(chainId, headerMap, queryMap, bodyMap);
        return new JsonBean<>(vo);
    }

}
